package jmm.threadPool.completionService;

import java.util.concurrent.*;

public class CompletionServiceDemo1_ {

    public static void main(String[] args) {
        // 创建线程池
        ExecutorService executor =
                Executors.newFixedThreadPool(3);

        CompletionService<String> completionService = new ExecutorCompletionService(executor);

        for (int i = 0; i < 5; i++) {
            int finalI = i;
            completionService.submit(()->{

                System.out.println("哈哈哈");
                return String.valueOf(finalI);
            });
        }

        String str = null;
        for (int i = 0; i < 8; i++) {
            try {
                //System.out.println(completionService.poll(2,TimeUnit.SECONDS));
                str = completionService.take().get();
                if (str != null){
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                
            }
        }
        System.out.println(str);

    }
}
